
using System;
using System.Collections;
using QdbUnit;
using QdbUnit.Extensions;
using QdbUnit.Extensions.Fixtures;
using QdbUnit.Extensions.Tests;
using QdbUnit.Extensions.Util;
using QdbUnit.Mocking;
using QdbUnit.Tests;
using Qdb.Foundation;
using Qdb.Foundation.IO;
using Sharpen.Lang;

namespace QdbUnit.Extensions.Tests
{
	public class FixtureTestCase : ITestCase
	{
		private sealed class ExcludingInMemoryFixture : QdbInMemory
		{
			public override bool Accept(Type clazz)
			{
				return !typeof(IOptOutFromTestFixture).IsAssignableFrom(clazz);
			}

			internal ExcludingInMemoryFixture(FixtureTestCase _enclosing)
			{
				this._enclosing = _enclosing;
			}

			private readonly FixtureTestCase _enclosing;
		}

		public virtual void TestSingleTestWithDifferentFixtures()
		{
			AssertSimpleQdb(new QdbInMemory());
			AssertSimpleQdb(new QdbSolo());
		}

		public virtual void TestMultipleTestsSingleFixture()
		{
			MultipleQdbTestCase.ResetConfigureCalls();
			FrameworkTestCase.RunTestAndExpect(new QdbTestSuiteBuilder(new QdbInMemory(), typeof(
				MultipleQdbTestCase)), 2, false);
			Assert.AreEqual(2, MultipleQdbTestCase.ConfigureCalls());
		}

		public virtual void TestSelectiveFixture()
		{
			IQdbFixture fixture = new FixtureTestCase.ExcludingInMemoryFixture(this);
			IEnumerator tests = new QdbTestSuiteBuilder(fixture, new Type[] { typeof(AcceptedTestCase
				), typeof(NotAcceptedTestCase) }).GetEnumerator();
			ITest test = NextTest(tests);
			Assert.IsFalse(tests.MoveNext());
			FrameworkTestCase.RunTestAndExpect(test, 0);
		}

		private void AssertSimpleQdb(IQdbFixture fixture)
		{
			IEnumerator tests = new QdbTestSuiteBuilder(fixture, typeof(SimpleQdbTestCase))
				.GetEnumerator();
			ITest test = NextTest(tests);
			MethodCallRecorder recorder = new MethodCallRecorder();
			SimpleQdbTestCase.RecorderVariable.With(recorder, new _IRunnable_46(test));
			recorder.Verify(new MethodCall[] { new MethodCall("fixture", new object[] { fixture
				 }), new MethodCall("configure", new object[] { MethodCall.IgnoredArgument }), new 
				MethodCall("store", new object[] {  }), new MethodCall("testResultSize", new object
				[] {  }) });
		}

		private sealed class _IRunnable_46 : IRunnable
		{
			public _IRunnable_46(ITest test)
			{
				this.test = test;
			}

			public void Run()
			{
				FrameworkTestCase.RunTestAndExpect(test, 0);
			}

			private readonly ITest test;
		}

		private ITest NextTest(IEnumerator tests)
		{
			return (ITest)Iterators.Next(tests);
		}

		public virtual void TestInterfaceIsAvailable()
		{
			Assert.IsTrue(typeof(IQdbTestCase).IsAssignableFrom(typeof(AbstractQdbTestCase)
				));
		}

		/// <exception cref="System.Exception"></exception>
		public virtual void TestDeleteDir()
		{
			System.IO.Directory.CreateDirectory("a/b/c");
			Assert.IsTrue(System.IO.File.Exists("a"));
			IOUtil.DeleteDir("a");
			Assert.IsFalse(System.IO.File.Exists("a"));
		}
	}
}
